package com.tbit.main.service.impl;

import com.tbit.main.constant.AccountDayStatConstant;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.stat.AccountDayStatDao;
import com.tbit.main.dao.stat.NewUserDayStaDao;
import com.tbit.main.pojo.AccountDayStat;
import com.tbit.main.pojo.NewUserDaySta;
import com.tbit.main.service.NewUserDayStaService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LMY
 * @create 2021-01-13 10:48
 * 新增用户统计
 */
@Service("newUserDayStaService")
public class NewUserDayStaServiceImpl implements NewUserDayStaService {
    @Autowired
    private NewUserDayStaDao newUserDayStaDao;
    @Autowired
    private AccountDayStatDao accountDayStatDao;

    @Override
    public List<NewUserDaySta> getByAccountIdDateDay(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataType = new LinkedList<>();
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_USERS);
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
        dataType.add(AccountDayStatConstant.NUMBER_OF_WEATHER);
        map.put("dataType", StringUtil.getListString(dataType));
        //return newUserDayStaDao.getByAccountIdDateDay(map);
        List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateDay(map);

        /**时间集合*/
        Set<Integer> date = new LinkedHashSet<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            date.add(accountDayStat.getDataDate());
        }

        /**数据集合*/
        Map<String, AccountDayStat> stringListHashMap = new HashMap<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            stringListHashMap.put(accountDayStat.getDataDate().toString() + "_" + accountDayStat.getDataType(), accountDayStat);

        }

        /**拼接数据*/
        List<NewUserDaySta> sumDTOList = new LinkedList<>();
        for (Integer key : date) {
            List<AccountDayStat> accountDayStatsList = new LinkedList<>();
            AccountDayStat accountDayStatNewUser = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_USERS);
            if (accountDayStatNewUser != null) {
                accountDayStatsList.add(accountDayStatNewUser);
            }
            AccountDayStat accountDayStatNewMile = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
            if (accountDayStatNewMile != null) {
                accountDayStatsList.add(accountDayStatNewMile);
            }
            AccountDayStat accountDayStatNewWeather = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_WEATHER);
            if (accountDayStatNewWeather != null) {
                accountDayStatsList.add(accountDayStatNewWeather);
            }
            NewUserDaySta userSumDTO = new NewUserDaySta(accountDayStatsList.get(0).getAccountId(), null, null, null,
                    DateTimeUtil.getYY_MM_DD(accountDayStatsList.get(0).getDataDate().toString()), accountDayStatsList.get(0).getAddTime(), accountDayStatsList.get(0).getDateWeek(), accountDayStatsList.get(0).getDateMonth());
            userSumDTO.setNewUserCount(accountDayStatNewUser == null ? 0L : accountDayStatNewUser.getSumTotal());
            userSumDTO.setNewMileCount(accountDayStatNewMile == null ? 0L : accountDayStatNewMile.getSumTotal());
            userSumDTO.setNewWeatherCount(accountDayStatNewWeather == null ? 0L : accountDayStatNewWeather.getSumTotal());
            sumDTOList.add(userSumDTO);
        }

        return sumDTOList;
    }

    @Override
    public List<NewUserDaySta> getByAccountIdDateWeek(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataType = new LinkedList<>();
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_USERS);
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
        map.put("dataType", StringUtil.getListString(dataType));
        //return newUserDayStaDao.getByAccountIdDateWeek(map);
        List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateWeek(map);

        /**最高天气*/
        Map<String, Object> mapWeatcher = new HashMap<>();
        mapWeatcher.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        mapWeatcher.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        mapWeatcher.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataTypeWeather = new LinkedList<>();
        dataTypeWeather.add(AccountDayStatConstant.NUMBER_OF_WEATHER);
        mapWeatcher.put("dataType", StringUtil.getListString(dataTypeWeather));
        List<AccountDayStat> accountDayStatsWeather = accountDayStatDao.getByAccountIdWeatherDateWeek(mapWeatcher);
        accountDayStats.addAll(accountDayStatsWeather);

        /**时间集合*/
        Set<Integer> date = new LinkedHashSet<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            date.add(accountDayStat.getDataDate());
        }

        /**数据集合*/
        Map<String, AccountDayStat> stringListHashMap = new HashMap<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            stringListHashMap.put(accountDayStat.getDataDate().toString() + "_" + accountDayStat.getDataType(), accountDayStat);

        }

        /**拼接数据*/
        List<NewUserDaySta> sumDTOList = new LinkedList<>();
        for (Integer key : date) {
            List<AccountDayStat> accountDayStatsList = new LinkedList<>();
            AccountDayStat accountDayStatNewUser = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_USERS);
            if (accountDayStatNewUser != null) {
                accountDayStatsList.add(accountDayStatNewUser);
            }
            AccountDayStat accountDayStatNewMile = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
            if (accountDayStatNewMile != null) {
                accountDayStatsList.add(accountDayStatNewMile);
            }
            AccountDayStat accountDayStatNewWeather = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_WEATHER);
            if (accountDayStatNewWeather != null) {
                accountDayStatsList.add(accountDayStatNewWeather);
            }
            NewUserDaySta userSumDTO = new NewUserDaySta(accountDayStatsList.get(0).getAccountId(), null, null, null,
                    DateTimeUtil.getYY_MM_DD(accountDayStatsList.get(0).getDataDate().toString()), accountDayStatsList.get(0).getAddTime(), accountDayStatsList.get(0).getDateWeek(), accountDayStatsList.get(0).getDateMonth());
            userSumDTO.setNewUserCount(accountDayStatNewUser == null ? 0L : accountDayStatNewUser.getSumTotal());
            userSumDTO.setNewMileCount(accountDayStatNewMile == null ? 0L : accountDayStatNewMile.getSumTotal());
            userSumDTO.setNewWeatherCount(accountDayStatNewWeather == null ? 0L : accountDayStatNewWeather.getSumTotal());
            sumDTOList.add(userSumDTO);
        }
        return sumDTOList;
    }

    @Override
    public List<NewUserDaySta> getByAccountIdDateMonth(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataType = new LinkedList<>();
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_USERS);
        dataType.add(AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
        map.put("dataType", StringUtil.getListString(dataType));
        //return newUserDayStaDao.getByAccountIdDateMonth(map);
        List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateMonth(map);

        /**最高天气*/
        Map<String, Object> mapWeatcher = new HashMap<>();
        mapWeatcher.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        mapWeatcher.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        mapWeatcher.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataTypeWeather = new LinkedList<>();
        dataTypeWeather.add(AccountDayStatConstant.NUMBER_OF_WEATHER);
        mapWeatcher.put("dataType", StringUtil.getListString(dataTypeWeather));
        List<AccountDayStat> accountDayStatsWeather = accountDayStatDao.getByAccountIdWeatherDateMonth(mapWeatcher);
        accountDayStats.addAll(accountDayStatsWeather);


        /**时间集合*/
        Set<Integer> date = new LinkedHashSet<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            date.add(accountDayStat.getDataDate());
        }

        /**数据集合*/
        Map<String, AccountDayStat> stringListHashMap = new HashMap<>();
        for (AccountDayStat accountDayStat : accountDayStats) {
            stringListHashMap.put(accountDayStat.getDataDate().toString() + "_" + accountDayStat.getDataType(), accountDayStat);

        }

        /**拼接数据*/
        List<NewUserDaySta> sumDTOList = new LinkedList<>();
        for (Integer key : date) {
            List<AccountDayStat> accountDayStatsList = new LinkedList<>();
            AccountDayStat accountDayStatNewUser = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_USERS);
            if (accountDayStatNewUser != null) {
                accountDayStatsList.add(accountDayStatNewUser);
            }
            AccountDayStat accountDayStatNewMile = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_NEW_MILEAGE);
            if (accountDayStatNewMile != null) {
                accountDayStatsList.add(accountDayStatNewMile);
            }
            AccountDayStat accountDayStatNewWeather = stringListHashMap.get(key + "_" + AccountDayStatConstant.NUMBER_OF_WEATHER);
            if (accountDayStatNewWeather != null) {
                accountDayStatsList.add(accountDayStatNewWeather);
            }
            NewUserDaySta userSumDTO = new NewUserDaySta(accountDayStatsList.get(0).getAccountId(), null, null, null,
                    DateTimeUtil.getYY_MM_DD(accountDayStatsList.get(0).getDataDate().toString()), accountDayStatsList.get(0).getAddTime(), accountDayStatsList.get(0).getDateWeek(), accountDayStatsList.get(0).getDateMonth());
            userSumDTO.setNewUserCount(accountDayStatNewUser == null ? 0L : accountDayStatNewUser.getSumTotal());
            userSumDTO.setNewMileCount(accountDayStatNewMile == null ? 0L : accountDayStatNewMile.getSumTotal());
            userSumDTO.setNewWeatherCount(accountDayStatNewWeather == null ? 0L : accountDayStatNewWeather.getSumTotal());
            sumDTOList.add(userSumDTO);
        }

        return sumDTOList;
    }

    @Override
    public Integer getByAccountId(Integer accountId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return newUserDayStaDao.getByAccountId(map);
    }

    @Override
    public Integer getByAccountIds(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return newUserDayStaDao.getByAccountIds(map);
    }

    @Override
    public Integer getAccountId(Integer accountId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return newUserDayStaDao.getAccountId(map);
    }
}
